<template>
  <div class="login-container">
    <!-- 标题导航栏 -->
    <van-navBar title="登录" left-arrow @click-left="goBack" />
    <!-- 登录表单 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <!-- 手机号文本框 -->
      <van-field
        v-model="user.mobile"
        name="mobile"
        label="手机号"
        placeholder="手机号"
        :rules="userRules.mobile"
      >
        <i slot="left-icon" class="iconfont icon-shouji"></i>
      </van-field>

      <!-- 验证码输入框 -->
      <van-field
        v-model="user.code"
        type="number"
        name="验证码"
        label="验证码"
        placeholder="验证码"
        :rules="userRules.code"
      >
        <i slot="left-icon" class="iconfont icon-yanzhengma"></i>
        <template #button>
          <!-- 倒计时器 -->
          <van-count-down
            v-if="isShowCount"
            :time="time"
            format="ss"
            @finish="isShowCount = false"
          />
          <!-- 发送验证码按钮 -->
          <van-button
            v-else
            round
            size="small"
            type="primary"
            native-type="button"
            @click="sendSms"
          >
            发送验证码
          </van-button>
        </template>
      </van-field>

      <!-- 提交按钮 -->
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendSMS } from '@/api/user'
export default {
  name: 'login',

  data () {
    return {
      // 1.表单内容数据
      user: {
        // 手机号
        mobile: '18581748606',
        // 验证码
        code: '246810'
      },
      // 2.表单验证规则
      userRules: {
        mobile: [
          { required: true, message: '请填写手机号', trigger: 'onBlur' },
          {
            pattern: /^1[3456789]\d{9}$/,
            message: '手机号不合法',
            trigger: 'onBlur'
          }
        ],
        code: [
          { required: true, message: '请填写验证码' },
          {
            pattern: /^\d{6}$/,
            message: '验证码不合法',
            trigger: 'onBlur'
          }
        ]
      },
      // 3.时间
      time: 30 * 1000,
      isShowCount: false
    }
  },

  mounted () {},

  methods: {
    // 登陆方法
    async onSubmit () {
      // 1.提示登录进行中
      this.$toast.loading({
        duration: 0, // 持续展示toast
        message: '加载中...',
        forbidClick: true // 是否禁止背景点击
      })
      try {
        // 2.发送登录请求
        const res = await login(this.user.mobile, this.user.code)
        console.log(res.data.data)
        // 3.提示登录成功
        this.$toast.success('登录成功')
        // 4.保存两个token到本地和Vuex仓库
        this.$store.commit('setUserToken', res.data.data)
        // 5.跳转到 #/ 组件
        this.$router.back()
      } catch (error) {
        console.log(error)
        // 4.提示登录失败
        this.$toast.fail('登录失败')
      }
    },
    // 发送短信验证码
    async sendSms () {
      // 使用try catch代替成功和失败回调函数
      try {
        // 执行校验，并返回一个 Promise对象
        await this.$refs.loginForm.validate('mobile')
      } catch (error) {
        // 校验失败，直接return，后面的代码就不执行了
        return console.log('校验失败了')
      }
      // 调用验证码接口
      try {
        await sendSMS(this.user.mobile)
        this.$toast.success('发送成功')
      } catch (error) {
        this.$toast.fail('发送失败')
        console.log('发送失败', error.message)
      }
    },
    // 返回
    goBack () {
      this.$router.back()
    }
  },
  computed: {},
  watch: {},
  components: {}
}
</script>

<style lang="less" scoped>
// body {
//   .van-nav-bar {
//     background-color: #0094ff;
//   }
// }
</style>
